﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QLKN.DataAccess;
using System.Text.RegularExpressions;

namespace QLKN.GUI
{
    public partial class frmNVxulydonKN : Form
    {
        int i;
        public frmNVxulydonKN(int manv)
        {
            InitializeComponent();
            i = manv;
        }
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        DataTable dt;
        public DataTable LayTrangthai()
        {
            conn = Data.getConnection();
            conn.Open();
            string sql = @"SELECT DISTINCT dTrangthai, TenTT=CASE 
                                        dTrangthai WHEN 'true' THEN N'Đã xử lý' 
                                        WHEN 'false' THEN N'Đang xử lý' else N'Chưa nhận' END FROM Donkhieunai";
            DataTable dt = Data.getDataTable(sql, conn);
            conn.Close();
            return dt;
        }
        private void frmNVxulydonKN_Load(object sender, EventArgs e)
        {
            conn = Data.getConnection();
            //hien thi don khieu nai chua tiep nhan
            colTrangthai.DataSource = LayTrangthai();
            colTrangthai.DisplayMember = "TenTT";
            colTrangthai.ValueMember = "dTrangthai";
            cmd = new SqlCommand(@"SELECT     Donkhieunai.dID, Donkhieunai.dLydokhieunai, Donkhieunai.dNgaylap, Donkhieunai.dNgayhentra, Donkhieunai.dTrangthai, Khachhang.kTenkhachhang, Sanpham.sTensanpham, Khachhang.kSodienthoai, Khachhang.kDiachi, Khachhang.kEmail
                                        FROM        Donkhieunai INNER JOIN
                                                    Khachhang ON Donkhieunai.kID = Khachhang.kID INNER JOIN
                                                    Sanpham ON Donkhieunai.sID = Sanpham.sID 
                                        WHERE nvID = "+i+" and dTrangthai is null", conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "donKN");
            SqlCommandBuilder cmb = new SqlCommandBuilder(da);
            dgvDonKNchuaxuly.DataSource = ds.Tables["donKN"];

            //hien thi don khieu nai dang xu ly
            colTrangthai2.DataSource = LayTrangthai();
            colTrangthai2.DisplayMember = "TenTT";
            colTrangthai2.ValueMember = "dTrangthai";
            cmd = new SqlCommand(@"SELECT     Donkhieunai.dID, Donkhieunai.dLydokhieunai, Donkhieunai.dNgaylap, Donkhieunai.dNgayhentra, Donkhieunai.dTrangthai, Khachhang.kTenkhachhang, Sanpham.sTensanpham, Khachhang.kSodienthoai, Khachhang.kDiachi, Khachhang.kEmail
                                        FROM        Donkhieunai INNER JOIN
                                                    Khachhang ON Donkhieunai.kID = Khachhang.kID INNER JOIN
                                                    Sanpham ON Donkhieunai.sID = Sanpham.sID 
                                        WHERE nvID = " + i + " and dTrangthai = 0", conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "donKN2");
            dgvDondangxuly.DataSource = ds.Tables["donKN2"];
            conn.Close();

            //hien thi don khieu nai da xu ly
            colTrangthai3.DataSource = LayTrangthai();
            colTrangthai3.DisplayMember = "TenTT";
            colTrangthai3.ValueMember = "dTrangthai";
            cmd = new SqlCommand(@"SELECT     Donkhieunai.dID, Donkhieunai.dLydokhieunai, Donkhieunai.dNgaylap, Donkhieunai.dNgayhentra, Donkhieunai.dTrangthai, Khachhang.kTenkhachhang, Sanpham.sTensanpham, Khachhang.kSodienthoai, Khachhang.kDiachi, Khachhang.kEmail
                                        FROM        Donkhieunai INNER JOIN
                                                    Khachhang ON Donkhieunai.kID = Khachhang.kID INNER JOIN
                                                    Sanpham ON Donkhieunai.sID = Sanpham.sID 
                                        WHERE nvID = " + i + " and dTrangthai = 1", conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "donKN3");
            dgvDaxuly.DataSource = ds.Tables["donKN3"];
            conn.Close();
        }

        private void btTiepnhan_Click(object sender, EventArgs e)
        {
            conn.Open();
            int dong = dgvDonKNchuaxuly.CurrentRow.Index;
            int dID = int.Parse(dgvDonKNchuaxuly.Rows[dong].Cells[0].Value.ToString());
            SqlCommand cmd = new SqlCommand("UPDATE Donkhieunai set dTrangthai = 0 WHERE dID=" + dID + "", conn);
            cmd.ExecuteNonQuery();
            frmNVxulydonKN_Load(sender, e);
            conn.Close();
        }

        private void btDaxuly_Click(object sender, EventArgs e)
        {
            conn.Open();
            int dong = dgvDondangxuly.CurrentRow.Index;
            int dID = int.Parse(dgvDondangxuly.Rows[dong].Cells[0].Value.ToString());
            SqlCommand cmd = new SqlCommand("UPDATE Donkhieunai set dTrangthai = 1 WHERE dID=" + dID + "", conn);
            cmd.ExecuteNonQuery();
            frmNVxulydonKN_Load(sender, e);
            conn.Close();
        }
    }
}
